﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FreeProperty.BusinessLogic;
using FreeProperty.Contract;
using System.Text;
using FreeProperty.Views.Shared;

namespace FreeProperty.Views.ProviderDirectory
{
    public partial class PrintResults : SiteBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session[FreePropertyConstants.SESSION_SEARCH_OBJECT])
            {
                Response.Redirect("~/Views/ProviderDirectory/SearchPassportProvider.aspx");
            }

            List<ProviderVO> lstResults = FreePropertyUtil.GetSearchResults();
            grdSearchResults.DataSource = lstResults;
            grdSearchResults.DataBind();

            
            lblResult.Text = lstResults.Count.ToString() + " providers were found matching your search criteria.";
            
        }
        
        

        protected void grdSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ProviderVO oProvider = (ProviderVO)e.Row.DataItem;
                GridViewRow row = e.Row;
                Image imgA = (Image)row.FindControl("imgServicesA");
                Image imgY = (Image)row.FindControl("imgServicesY");
                Image imgC = (Image)row.FindControl("imgServicesC");

                imgA.Visible = false;
                imgY.Visible = false;
                imgC.Visible = false;

                //Provider objProvider = new Provider();
                //var list = objProvider.SearchProviders();
                 if (oProvider.Services == "A")
                        imgA.Visible = true;
                    else if (oProvider.Services == "Y")
                        imgY.Visible = true;
                    else if (oProvider.Services == "C")
                        imgC.Visible = true;

                HyperLink  lnkMap = (HyperLink)row.FindControl("lnkMap");
                StringBuilder sbAddress = new StringBuilder();
                sbAddress.Append(oProvider.Address).Append(" ").Append(oProvider.City).Append(" ").Append(oProvider.State).Append(" ").Append(oProvider.ZipCode);

                var url = "http://maps.google.co.in/maps?f=q&source=s_q&hl=en&geocode=&q=" + HttpContext.Current.Server.UrlEncode(sbAddress.ToString());
                lnkMap.NavigateUrl = url;

                // Distance showing logic 
                if (null != Session[FreePropertyConstants.SESSION_SEARCH_OBJECT])
                {
                    SearchParametersVO oSearchParametersVO = (SearchParametersVO)Session[FreePropertyConstants.SESSION_SEARCH_OBJECT];
                    if (oSearchParametersVO.IsDistanceSearch)
                    {
                        ZipCodeVO zipProvider = oSearchParametersVO.ListDistanceZipCodes.Find(t => t.ZipCode.Equals(oProvider.ZipCode));
                        if (zipProvider != null)
                        {
                            Label lblDistanceInfo = (Label)row.FindControl("lblDistanceInfo");
                            lblDistanceInfo.Text = zipProvider.DistanceInMiles + " mile(s) from Zipcode " + oSearchParametersVO.Zipcode + " .";
                        }
                    }
                }
                


            }

        }

       
        
    }
}